/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.epam.expertsys.web;

import java.util.Properties;
import java.sql.SQLException;
import java.io.IOException;
import org.apache.log4j.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.epam.expertsys.dbconn.*;

/**
 * Web application lifecycle listener.
 * @author Admin
 */
public class ExsysContextListener implements ServletContextListener {

    private static Logger log = Logger.getLogger(ExsysContextListener.class);

    @Override
    public void contextInitialized(ServletContextEvent sce) {



        ServletContext servContext = sce.getServletContext();
        int maxConn = 50;
        try {
            maxConn = Integer.parseInt(servContext.getInitParameter("maxDbConnectionsNumber"));
        } catch (NumberFormatException e) {
            //intentionally left blank
        }

        PatientDAOH2 patDAO = null;
        AmbulatoryRecordDAOH2 ambDAO = null;
        RequestDAOH2 reqDAO = null;

        try {
            patDAO = new PatientDAOH2(maxConn);
            ambDAO = new AmbulatoryRecordDAOH2(maxConn);
            reqDAO = new RequestDAOH2(maxConn);
        } catch (SQLException ex) {
            //TODO: exc. logic
        }

        servContext.setAttribute("patientDAO", patDAO);
        servContext.setAttribute("ambulatoryRecordDAO", ambDAO);
        servContext.setAttribute("requestDAO", reqDAO);

        log.info("Context initialized");

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("Context destroyed");
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}
